package com.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.app.util.Utils_Image;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;

import java.util.Timer;
import java.util.TimerTask;




// 


public class ImageViewMarquee extends ImageView implements android.view.ViewTreeObserver.OnPreDrawListener{



	ImageViewMarquee view = this;
	Timer mTimer = new Timer();


	Paint paint ;
	private boolean haveNotice = true;
	private int scroolTime = 33;   //滚动的

	int width ;
	int width_view ;

	int height;


	//
	float x = 0;
	float y = 0;

	Bitmap bitmap_source;
	Bitmap bitmap;


	public ImageViewMarquee(Context context) {
		super(context);
		// TODO Auto-generated constructor stub

		init();
	}

	public ImageViewMarquee(Context context, AttributeSet attrs){
		super(context, attrs);
		
		/*
		  if (null != attrs) {
	            TypedArray ta = context.obtainStyledAttributes(attrs, );

	            if (ta != null) {
	            	
	      
	            	
	                ta.recycle();
	            }
	        }
	        else{
	        	
	        }
		  */
		
		init();
	}
	
	
	
	void init(){
		
		
		paint = new Paint();

		 
		 getViewTreeObserver().addOnPreDrawListener(this);
		 
		
	}
	
 
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		//super.onDraw(canvas);   // 不需要父控件显示出文字内容
		

		canvas.drawBitmap(bitmap, x, y, paint);

		
		
	}
	
	

	
	// 循环控制器
	class TimerTaskHorizontal extends TimerTask
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub



			if(width < width_view)
				return;

			
			for(;Math.abs(x)<width; x-=2){    //偏移x


				/*
				// img_width - screen_width < screen_width
				// copy头部图片到空白区域；   img_width[space_width] -> bitmap
				// canvas.drawbitmap(bitmap, width-space_width, y, paint);
				if( width-Math.abs(x) < width_view){
					int delta_x = (width-(int)Math.abs(x));
					int space_x = width_view - delta_x;
					Canvas canvas = new Canvas(bitmap);
					//canvas.drawBitmap(bitmap_source, space_x, y, paint);

					Rect src = new Rect();
					Rect dest = new Rect();

					src.left = space_x;
					src.top = 0;
					src.right = src.left + width;
					src.bottom = src.top + height;

					dest.left = 0;
					dest.top = 0;
					dest.right = delta_x;
					dest.bottom = height;

					canvas.drawBitmap(bitmap_source, src, dest, paint);


				}
				*/

				if(x <= width/2){ //如果到达一半
					
				}
				
				try {
					Thread.sleep(scroolTime);  // 使移动的速度，慢下来
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				handler.sendEmptyMessage(1);  //刷新
				
			}
			
			
			//if (showContent_horizontal >= width) 
			{
				handler_horizontal(); // 开启新的循环
				
				// 使变量复原
				x = 0;
				//showContent_horizontal = 0;
			}
			
		}
		
	};

	
	public void handler_horizontal() {	
		
		if (haveNotice) {
			
			mTimer = new Timer();
			mTimer.schedule(new TimerTaskHorizontal(), 50);  //50ms后开始执行
		}
	}
	
	
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			invalidate();
		};
	};



	// 首次对文本进行处理形成text+"   "+text的形式
	@Override
	public boolean onPreDraw() {
		// TODO Auto-generated method stub


		BitmapDrawable drawable = (BitmapDrawable)getDrawable();
		bitmap_source = drawable.getBitmap();


		bitmap = Bitmap.createBitmap(bitmap_source.getWidth(), bitmap_source.getHeight(), bitmap_source.getConfig());
		Canvas canvas = new Canvas(bitmap);
		canvas.drawBitmap(bitmap_source, 0, 0, null);

		//bitmap = Bitmap.createBitmap(bitmap_source);
		//bitmap = Utils_Image.add2Bitmap(bitmap_local, bitmap_local);

		width = bitmap.getWidth();
		width_view = view.getMeasuredWidth();
		height = bitmap.getHeight();



		/*
		StringBuilder sb = new StringBuilder();
		sb.append(context);
		for(;;){
			float width_with_white = paint_txt.measureText(sb.toString());
			if(width_with_white >= width){
				break;
			}
			
			sb.append(" ");
			
		}
		
		context = sb.toString() + context;
		*/


		/*
		Rect bounds = new Rect();
		paint_txt.getTextBounds(context, 0, context.length(), bounds);
		y = height/2 + bounds.height()/2;  // 在空间中居中显示
		*/

		
		handler_horizontal();

		view.getViewTreeObserver().removeOnPreDrawListener(view); //首次测量后不需要再监听了；
		
		return true;
		
	}
	

}
